package com.yunyi.camunda.controller;

import com.yunyi.camunda.common.utils.UserUtils;
import com.yunyi.camunda.pojo.Result;
import com.yunyi.camunda.pojo.dto.User;
import com.yunyi.camunda.pojo.vo.CompleteTaskRequest;
import com.yunyi.camunda.pojo.vo.RejectTaskRequest;
import com.yunyi.camunda.pojo.vo.StartProcessRequest;
import com.yunyi.camunda.pojo.vo.TransferTaskRequest;
import com.yunyi.camunda.service.ProcessInstanceService;
import com.yunyi.camunda.service.ProcessTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 流程部署controller
 *
 * @author yunyi
 */
@RequestMapping("/process/instance")
@RestController
public class ProcessInstanceController {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Autowired
    private ProcessTaskService processTaskService;


    /**
     * 根据流程定义id发起流程实例
     *
     * @param requestParam 请求参数
     * @return 流程实例id
     */
    @PostMapping("/startProcessInstanceById")
    public Result<String> startProcessInstanceById(@RequestBody StartProcessRequest requestParam) {
        User userInfo = UserUtils.getVirtualUser();
        return processInstanceService.startProcessInstanceById(userInfo, requestParam);
    }


    /**
     * 完成单个任务
     *
     * @param requestParam 请求参数
     * @return 任务所在节点信息
     */
    @PostMapping("/completeSingleTask")
    public Result<Map<String, String>> completeSingleTask(@RequestBody @Validated CompleteTaskRequest requestParam) {
        return processTaskService.completeSingleTask(requestParam);
    }

    /**
     * 转办任务
     *
     * @param requestParam 请求参数
     */
    @PostMapping("/transferTask")
    public Result<Void> transferTask(@RequestBody @Validated TransferTaskRequest requestParam) {
        return processTaskService.transferTask(requestParam);
    }

    /**
     * 驳回到上个节点
     *
     * @param requestParam 请求参数
     */
    @PostMapping("/reject")
    public Result<Void> reject(@RequestBody @Validated RejectTaskRequest requestParam) {
        return processTaskService.rejectTask(requestParam);
    }


    /**
     * 领取任务
     *
     * @param taskId 任务id
     * @return 提示信息
     */
    @GetMapping("/claimTask")
    public Result<String> claimTask(String taskId) {
        return processTaskService.claimTask(taskId);
    }


}
